package com.rcl.mvvmdemo

import java.util.*

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/11/4.
 * PS: Not easy to write code, please indicate.
 */
class UserModel {

    companion object {
        const val STATE_LOGIN_LOADING = 0
        const val STATE_LOGIN_SUCCESS = 1
        const val STATE_LOGIN_FAILED = 2
    }

    private val api by lazy {
        API()
    }

    private val random = Random()

    /**
     * 进行登录操作
     */
    fun doLogin(account: String, password: String, block: (Int) -> Unit) {
        block.invoke(STATE_LOGIN_LOADING)
        //开始去调用登录API
        //有结果，此操作为耗时操作
        //向服务器提交数据，这个时候，会使用非主线程去操作
        //异步操作，通知UI的时候，会使用非主线程去操作
        //因为UI只能在主线程里更新
        //0,1
        val randomValue = random.nextInt(2)
        if (randomValue == 0) {
            block.invoke(STATE_LOGIN_SUCCESS)
        } else {
            block.invoke(STATE_LOGIN_FAILED)
        }
    }

    fun checkUserStatus(account: String, block: (Int) -> Unit) {
        //0表示该账号已经注册
        //1表示该账号没有注册
        block.invoke(random.nextInt(2))
    }
}